package com.wuyoucar.controller;

import com.wuyoucar.common.ResultBean;
import com.wuyoucar.service.InTPolicyService;
import com.wuyoucar.service.InTReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Date;
import java.util.UUID;

/**
 * @Author: jiangdz
 * @ProjectName: mywuyou_car
 * @Package: com.wuyoucar.controller
 * @ClassName: TPolicyController
 * @Description:
 * @Date: 2020-09-24  11:50
 * @UpdateUser: asus
 * @UpdateDate: 2020-09-24  11:50
 * @UpdateRemark: [说明本次修改内容]
 * @Version: [v1.0]
 * @Since JDK 1.8
 */
@RestController
@RequestMapping("/policy")
public class TPolicyController {
    @Autowired
    InTPolicyService inTPolicyService;

    @Autowired
    InTReportService inTReportService;

    // 项目根路径下的目录  -- SpringBoot static 目录相当于是根路径下（SpringBoot 默认）
    public final static String UPLOAD_PATH_PREFIX = "static/claims/";

    //查看我的保单
    @RequestMapping("/getPolicy")
    public ResultBean getPolicy(Integer userid) {
        return inTPolicyService.getPolicy(userid);
    }

    //加保
    @RequestMapping("/addPolicy")
    public ResultBean addPolicy(Integer policyno, Integer year) {
        return inTPolicyService.addPolicy(policyno, year);
    }

    //上传图片
    @RequestMapping("/upload")
    public ResultBean cardText(@RequestParam(value = "img", required = false) MultipartFile img, HttpServletRequest request) {
        if (img.isEmpty()) {
            //返回选择文件提示
            return new ResultBean(0, "请选择上传文件", "");
        }
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd/");
        //构建文件上传所要保存的"文件夹路径"--这里是相对路径，保存到项目根路径的文件夹下
        String realPath = new String("client/src/main/resources/" + UPLOAD_PATH_PREFIX);
//        String format = sdf.format(new Date());
        //存放上传文件的文件夹
        File file = new File(realPath);
        if (!file.isDirectory()) {
            //递归生成文件夹
            file.mkdirs();
        }
        //获取原始的名字  original:最初的，起始的  方法是得到原来的文件名在客户机的文件系统名称
        String oldName = img.getOriginalFilename();
        String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."), oldName.length());
        try {
            //构建真实的文件路径
            File newFile = new File(file.getAbsolutePath() + File.separator + newName);
            //转存文件到指定路径，如果文件名重复的话，将会覆盖掉之前的文件,这里是把文件上传到 “绝对路径”
            img.transferTo(newFile);
            //文件保存路径
            String filePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/claims/" + newName;

            // 上传成功返回文件路径(包含文件名)
            return new ResultBean(1, "上传成功", newName);
            //将上传的图片名称保存至数据库

        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResultBean(0, "请选择清晰的图片上传", "");
    }

    //申请理赔
    @RequestMapping("/claim")
    public ResultBean addReport(String imgPath, @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date risktime,
                                @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date reporttime, Integer policynum,Integer userid,
                                String reportertel) {
        return inTReportService.addReport(imgPath, risktime, reporttime, policynum,userid,reportertel);
    }

    //查看保单号
    @RequestMapping("/selectPolicyno")
    public ResultBean selectPolicyno(Integer policyno, Integer userid) {
        return inTPolicyService.selectPolicyno(policyno, userid);
    }
}
